
public class LoginManager {
    private static final int PASSWORD_LENGTH = 5;

    private String username = null;
    private boolean loggedIn = false;
    private ChatManager chatManager;
    ChatFile userFile = new ChatFile(LoginManager.class.getResource("LoginManager.class").getFile() + "userfile");
    public String loginString;
    public LoginManager(ChatManager manager) {
        chatManager = manager;
    }
    public void setLoggedIn(boolean logged)
    {
        loggedIn = logged;
    }

    public String setLoginString(String login)
    {
        loginString = login;
        return loginString;
    }
    public String check(String str) {
        // For a user to start the login process, they must type "username:"
        // Although this may seem ugly, it will keep out those who aren't
        // taught the login system.
        if (str.startsWith("username:")) {
            String user = str.replaceFirst("username: *", "");
            if (user.length() > 2) {
                username = user;
                return "Enter your password.";
            }
        } else if (username != null) {
            String truePassword = getDesiredPassword(username);
            System.out.println(truePassword); // for testing purposes
            if (str.equals(truePassword)) {
                chatManager.setUsername(username);
                loginString = username+ ','+String.valueOf(System.currentTimeMillis())+'\n';
                userFile.append(loginString);
                System.out.println(loginString);
                loggedIn = true;
                return "Password correct.";
            } else {
                // If the password is incorrect, the user must repeat the
                // login process.
                username = null;
                return "Incorrect password. Re-enter username.";
            }
        }
        return null;
    }

    protected String getDesiredPassword(String input) { //make this more fun if you want
        String rotated = rot13(input);

        while (rotated.length() < PASSWORD_LENGTH) {
            rotated += rotated;
        }
        return addNumbers(rotated.substring(0, PASSWORD_LENGTH));
    }

    // ceasar's cipher
    public static String rot13(String str) {
        StringBuilder builder = new StringBuilder(str.length());
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            if      (c >= 'a' && c <= 'm') c += 13;
            else if (c >= 'n' && c <= 'z') c -= 13;
            else if (c >= 'A' && c <= 'M') c += 13;
            else if (c >= 'A' && c <= 'Z') c -= 13;

            builder.append(c);
        }
        return builder.toString();
    }

    // turn every odd letter into a number
    public static String addNumbers(String str) {
        boolean changeEvens = true;
        StringBuilder builder = new StringBuilder(str.length());
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            boolean isEven = (c % 2) == 0;

            if (isEven == changeEvens) {
                changeEvens = !changeEvens;
                builder.append((c & 0x1F) % 10);
            } else {
                builder.append(c);
            }
        }
        return builder.toString();
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }
}
